package world;

import java.util.Arrays;

public class Chunk {
	public final static int SIZE = 16;
	
	private ChunkSlice[] sliceArray;
	private byte[] biomeArray;

/*	private boolean loadedYN;
	private boolean modifiedYN;
	private long lastSaveTime;*/

	private final int xPos;
	private final int zPos;

	private World world;	
	public Chunk(World world, int xPos, int zPos) {
		this.world = world;
		this.xPos = xPos;
		this.zPos = zPos;

		this.sliceArray = new ChunkSlice[16];
		this.biomeArray = new byte[256];
		Arrays.fill(this.biomeArray, (byte) -1);
	}

	public Chunk(World world, byte[] byteArrayData, int xPos, int zPos) {
		this(world, xPos, zPos);
		int k = byteArrayData.length / 256;

		for (int xi = 0; xi < ChunkSlice.X_SIZE; ++xi) {
			int xBase = xi << 11;
			for (int zi = 0; zi < ChunkSlice.Z_SIZE; ++zi) {
				int zBase = zi << 7;
				for (int yi = 0; yi < k; ++yi) {
					int blockID = byteArrayData[xBase | zBase | yi] & 0xFF;

					if (blockID != 0) {
						int yBasePos = yi >> 4;

						if (this.sliceArray[yBasePos] == null) {
							this.sliceArray[yBasePos] = new ChunkSlice(yBasePos << 4, false);
						}

						this.sliceArray[yBasePos].setBlockID(xi, yi & 15, zi, blockID);
					}
				}
			}
		}
	}

	public boolean atLocation(int xPos, int zPos) {
		return xPos == this.xPos && zPos == this.zPos;
	}	

	public ChunkSlice getTopSlice() {
		for (int i = this.sliceArray.length - 1; i >= 0; --i) {
			if (this.sliceArray[i] != null) {
				return this.sliceArray[i];
			}
		}

		return null;
	}
	
    
	public int getBlockID(int xPos, int yPos, int zPos) {
		int yIndex = yPos >> 4;
		if (yIndex >= this.sliceArray.length) {
			return 0;
		}

		ChunkSlice slice = this.sliceArray[yIndex];
		if (slice != null) {
			slice.getBlockID(xPos, yPos & 15, zPos);
		}
		return 0;
	}

   
	public boolean isAllSlicesEmpty(int par1, int par2) {
		if (par1 < 0) {
			par1 = 0;
		}

		if (par2 >= 256) {
			par2 = 255;
		}

		for (int k = par1; k <= par2; k += 16) {
			ChunkSlice slice = this.sliceArray[k >> 4];

			if (slice != null && !slice.isEmpty()) {
				return false;
			}
		}

		return true;
	}
	//----------------------------------------------------------------------getter setter
	public ChunkSlice[] getSliceArray() {
		return sliceArray;
	}

	public void setSliceArray(ChunkSlice[] sliceArray) {
		this.sliceArray = sliceArray;
	}

	public World getWorld() {
		return world;
	}

	public ChunkBlock findBlock(int posInChunkX, int posInChunkY, int posInChunkZ) {
		// TODO Auto-generated method stub
		return null;
	}

	public static int getID(int chunkIndexX, int chunkIndexZ) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setBlocks(ChunkBlock[][][] createBlocks) {
		// TODO Auto-generated method stub
		
	}

	public int getIndexX() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void buildRenderData() {
		// TODO Auto-generated method stub
		
	}

	public Integer getID() {
		// TODO Auto-generated method stub
		return null;
	}
}
